home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / comm / ytsg3.zip / GETOPT.C < prev    next >
Text File  |  1993-05-05  |  7KB  |  141 lines

  1. /************************************************/
  2. /* Author: G.R. Blair                           */
  3. /*         BOBBLAIR @ AUSVM1                    */
  4. /*         bobblair@bobblair.austin.ibm.com     */
  5. /*                                              */
  6. /* The following code is property of            */
  7. /* International Business Machines Corporation. */
  8. /*                                              */
  9. /* Copyright International Business Machines    */
  10. /* Corporation, 1991.  All rights reserved.     */
  11. /************************************************/
  12.  
  13. /* -------------------------------------------------------------------------- */
  14. /* getopt()                                                                   */
  15. /*                                                                            */
  16. /* The getopt() function is a command line parser.  It returns the next       */
  17. /* option character in argv that matches an option character in opstring.     */
  18. /*                                                                            */
  19. /* The argv argument points to an array of argc+1 elements containing argc    */
  20. /* pointers to character strings followed by a null pointer.                  */
  21. /*                                                                            */
  22. /* The opstring argument points to a string of option characters; if an       */
  23. /* option character is followed by a colon, the option is expected to have    */
  24. /* an argument that may or may not be seperated from it by white space.  The  */
  25. /* external variable optarg is set to point to the start of the option        */
  26. /* argument on return from getopt().                                          */
  27. /*                                                                            */
  28. /* The getopt() function places in optind the argv index of the next argument */
  29. /* to be processed.  The system initializes the external variable optind to   */
  30. /* 1 before the first call to getopt().                                       */
  31. /*                                                                            */
  32. /* When all options have been processed (that is, up to the first nonoption   */
  33. /* argument), getopt() returns EOF.  The special option "--" may be used to   */
  34. /* delimit the end of the options; EOF will be returned, and "--" will be     */
  35. /* skipped.                                                                   */
  36. /*                                                                            */
  37. /* The getopt() function returns a question mark (?) when it encounters an    */
  38. /* option character not included in opstring.  This error message can be      */
  39. /* disabled by setting opterr to zero.  Otherwise, it returns the option      */
  40. /* character that was detected.                                               */
  41. /*                                                                            */
  42. /* If the special option "--" is detected, or all options have been           */
  43. /* processed, EOF is returned.                                                */
  44. /*                                                                            */
  45. /* No errors are defined.                                                     */
  46. /* -------------------------------------------------------------------------- */
  47. /* OS/2 implementation (by G.R. Blair)                                        */
  48. /*                                                                            */
  49. /* Options are marked by either a minus sign (-) or a slash (/).              */
  50. /* -------------------------------------------------------------------------- */
  51. /* Maintenance History:                                                       */
  52. /* @1  02/17/92  grb  Peter Schwaller pointed out that the code walks off the */
  53. /*                    end of argv in some circumstances.                      */
  54. /*                                                                            */
  55. /* -------------------------------------------------------------------------- */
  56.  
  57. #include <stdio.h>                   /* for EOF */
  58. #include <string.h>                  /* for strchr() */
  59.  
  60.  
  61. /* static (global) variables that are specified as exported by getopt() */
  62. char *optarg = NULL;    /* pointer to the start of the option argument  */
  63. int   optind = 1;       /* number of the next argv[] to be evaluated    */
  64. int   opterr = 1;       /* non-zero if a question mark should be returned
  65.                            when a non-valid option character is detected */
  66.  
  67. #define COLON        ':'
  68. #define QUESTIONMARK '?'
  69. #define SLASH        '/'
  70. #define MINUS        '-'
  71. #define ISOPT(p) ((*p == SLASH) || (*p == MINUS))
  72. #define BADOPTERROR(c)  optind++, return( opterr ? QUESTIONMARK : c )
  73.  
  74. int getopt(int argc, char *argv[], char *opstring)
  75. {
  76.   static char *indpos = NULL;
  77.   char c, *p, *q;
  78.  
  79.  
  80.   p = NULL;
  81.   if (indpos)
  82.     if (*(++indpos))
  83.       p = indpos;
  84.   if (p == NULL)
  85.     {
  86.       if (optind >= argc)
  87.         {
  88.           indpos = NULL;
  89.           return(EOF);
  90.         }
  91.       p = argv[optind++];
  92.       if (!ISOPT(p))         /* If the next argv[] is not an option */
  93.         {                    /*   there can be no more options      */
  94.           --optind;            /* we point to the current arg once we're done*/
  95.           optarg = NULL;
  96.           indpos = NULL;
  97.           return(EOF);
  98.         }
  99.                              /* check for special end-of-flags markers */
  100.       if ((strcmp(p, "-") == 0) || (strcmp(p, "--") == 0))
  101.         {
  102.            optarg = NULL;
  103.            indpos = NULL;
  104.            return(EOF);
  105.         }
  106.       p++;
  107.     }
  108.   if (*p == COLON)
  109.      return(opterr ? QUESTIONMARK : c);
  110.   else
  111.      if ((q = strchr(opstring, *p)) == 0)
  112.         {
  113.            optarg = NULL;
  114.            indpos = NULL;
  115.            return(opterr ? QUESTIONMARK : c);
  116.         }
  117.      else
  118.         {
  119.           if (*(q + 1) != COLON)
  120.              {
  121.                 optarg =  NULL;        /* no argument follows the option */
  122.                 indpos = p;
  123.              }
  124.           else
  125.              {
  126.                 if (*(p + 1) != '\0')  /* Arg follows.  Is it in this argv? */
  127.                    optarg = ++p;            /* Yes, it is */
  128.                 else
  129.                   if (optind < argc)                               /* @1a */
  130.                     optarg = argv[optind++]; /*No, it isn't--it is in the next*/
  131.                   else                                             /* @1a */
  132.                     {              /* or it doesn't exist at all      @1a */
  133.                        optarg = NULL;                              /* @1a */
  134.                        return(opterr ? QUESTIONMARK : c);          /* @1a */
  135.                     }                                              /* @1a */
  136.                 indpos = NULL;
  137.              }
  138.           return(*q);
  139.         }
  140. }
  141.